﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsFormsApplication.Class;

namespace WindowsFormsApplication
{
    public partial class BindingTest : Form
    {
        List<BindingSourcePerson> lstPerson = new List<BindingSourcePerson>();
        BindingManagerBase bindingManager;
        BindingSource bs;
        Dictionary<string, BindingSourcePerson> personHeap = new Dictionary<string, BindingSourcePerson>();
        public BindingTest()
        {
            InitializeComponent();
        }

        private void BindingTest_Load(object sender, EventArgs e)
        {
            lstPerson.Add(new BindingSourcePerson(new Person("Alex", "John", 23,DateTime.Now)));
            lstPerson.Add(new BindingSourcePerson(new Person("Jack", "Jone", 25, DateTime.Now.Add(new TimeSpan(1,1,0,0)))));
            lstPerson.Add(new BindingSourcePerson(new Person("Mike", "Will", 26, DateTime.Now.Add(new TimeSpan(2,2,0,0)))));
            lstPerson.Add(new BindingSourcePerson(new Person("Gill", "Jack", 30, DateTime.Now.Add(new TimeSpan(3,3,0,0)))));

            bs = new BindingSource();
            bs.DataSource = lstPerson;
            this.listBox1.DataSource = bs;
            this.listBox1.DisplayMember = "FirstName";
            this.listBox1.ValueMember = "Age";

            bindingManager = this.BindingContext[bs];

            this.textBox1.DataBindings.Add("TEXT", bs, "FirstName", true, DataSourceUpdateMode.OnPropertyChanged);
            this.textBox2.DataBindings.Add("TEXT", bs, "LastName", true, DataSourceUpdateMode.OnPropertyChanged);
            this.textBox3.DataBindings.Add("TEXT", bs, "Age", true, DataSourceUpdateMode.OnPropertyChanged);
            this.monthCalendar1.MaxSelectionCount = 1;
            this.monthCalendar1.DataBindings.Add("SelectionRange", bs, "Date", true, DataSourceUpdateMode.OnPropertyChanged);
            this.dateTimePicker1.DataBindings.Add("Value", bs, "Time", true, DataSourceUpdateMode.OnPropertyChanged);
            bindingManager.Position = (int)listBox1.SelectedIndex + 1;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            lstPerson.Add(new BindingSourcePerson(new Person("wu", "di", 27,DateTime.Now.Add(new TimeSpan(4,4,0,0)))));

            //TODO: This ResetBindings is very important, it will raise event ListChanged to notify the ListBox to update UI.
            bs.ResetBindings(false);
            //listBox1.Refresh();
            //listBox1.Parent.Refresh();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //bindingManager.Position = (int)listBox1.SelectedIndex + 1;

            GC.Collect();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            lstPerson.RemoveAt(0);
            bs.ResetBindings(false);
        }
    }
}
